package com.learn.admin.common.service;

import com.learn.admin.common.persistence.BaseDao;
import com.learn.admin.common.persistence.DataEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wgc on 2017/1/20.
 */
@Transactional(readOnly = true)
public abstract class CrudService<D extends BaseDao<T>, T extends DataEntity<T>> extends BaseService {

    @Autowired
    protected D dao;

    public T get(String id) {
        return dao.get(id);
    }

    public T get(T entity) {
        return dao.get(entity);
    }

    // 查询分页数据：findPage

    @Transactional(readOnly = false)
    public void save(T entity) {
        if (entity.getIsNewRecord()) {
            entity.preInsert();
            dao.insert(entity);
        } else {
            entity.preUpdate();
            dao.update(entity);
        }
    }

    @Transactional(readOnly = false)
    public void delete(T entity) {
        dao.delete(entity);
    }
}
